<template>
	<view class="content">
		<image class="login-title" src="@/static/logo.png" mode=""></image>

		<view class="login-box">
			<view class="input-box">
				<text class="input-title">账号</text>
				<input maxlength="20" class="input" v-model="loginForm.username" placeholder-class="input-placeholder" type="text"
				 placeholder="请输入账号" />
			</view>
			<view class="input-box">
				<text class="input-title">密码</text>
				<input maxlength="12" class="input" v-model="loginForm.password" placeholder-class="input-placeholder" type="password"
				 placeholder="请输入密码" />
			</view>
			<button class="btn-login" type="default" @click="handleLogin()">登录</button>
		</view>
	</view>
</template>

<script>
	import { login, getUserInfo } from '@/api/user.js'
	export default {
		data() {
			return {
				loginForm: {
					username: 'admin',
					password: 'admin123',
				}
			}
		},
		onLoad() {
			console.log('token:'+this.$store.getters.token)
			if(this.$store.getters.token){
				this.initUser();
			}
		},
		methods: {
			handleLogin() {
				var p = null;
				this.$store.commit('SET_ROLE', this.iden)

				for(var v in this.loginForm){
					if (!v) {
						this.errorMsg('账号密码不能为空');
						return;
					}
				}

				login(this.loginForm.username, this.loginForm.password).then(res => {
					uni.setStorageSync('token', res.token);
					this.$store.commit('SET_TOKEN', res.token);

					this.initUser();
				});
			},
			initUser(){
				this.$store.dispatch('getUserInfo').then(res => {
					uni.reLaunch({
						url: '/pages/index/index',
					})
				});
			}
		},
	}
</script>

<style scoped>
	.content {
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: #fff;
	}

	.content .login-title {
		display: block;
		margin: 0 auto;
		width: 60%;
		padding-top: 20;
	}

	.login-box {
		margin-top: 47rpx;
		padding: 0 65rpx;
	}

	.login-box .input-box .input-title {
		display: block;
		color: #b5b5b5;
		font-size: 28rpx;
		margin-bottom: 17rpx;
	}

	.login-box .input-box .input {
		height: auto;
		padding: 20rpx 0;
		color: #7d7d7d;
		font-size: 32rpx;
		border-bottom: 1px solid #8e8e8e;
	}

	.login-box .input-box .input-placeholder {
		color: #7d7d7d;
		font-size: 32rpx;
	}

	.btn-login {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 600rpx;
		height: 90rpx;
		background: #3fc5be;
		border-radius: 10rpx;
		border: none;
		color: #ffffff;
		font-size: 36rpx;
		margin-top: 60rpx;
	}

	.line {
		margin-top: 5px;
		margin-bottom: 20px;
	}
</style>
